package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.comment.response.JsonResult;
import cn.tedu._05mvcboot03.comment.response.StatusCode;
import cn.tedu._05mvcboot03.mapper.Mvc_articleMapper;
import cn.tedu._05mvcboot03.pojo.DTO.UserArticleDTO;
import cn.tedu._05mvcboot03.pojo.entity.Mvc_article;
import cn.tedu._05mvcboot03.pojo.vo.ArticleVO;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "02.文章模块")
@Slf4j
@RestController
@Validated
@RequestMapping("/v1/users/")
public class Mvc_articleController {
    @Autowired
    public Mvc_articleMapper mvcArticleMapper;
    @PostMapping("article")
    @ApiOperation(value = "发表文章功能")
    public JsonResult article(@ApiIgnore HttpSession session,@Autowired @Validated UserArticleDTO userArticleDTO){
        log.debug("userArticleDTO:" + userArticleDTO);
        UserVO userVO =(UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Mvc_article mvcArticle = new Mvc_article();
        BeanUtils.copyProperties(userArticleDTO,mvcArticle);
        mvcArticle.setUserId(userVO.getId());
        log.debug("mvcArticle = " + mvcArticle);
        mvcArticleMapper.insertArticle(mvcArticle);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }
    @GetMapping("selectByArticleId")
    @ApiOperation(value = "查询文章功能")
    @ApiImplicitParam(name = "id",value ="文章编号",required = true,dataType = "long")
    public JsonResult selectByArticleId(@Range(min = 1,max = 100,message = "文章id必须在1-100之间") @RequestParam Long id){
        log.debug("id = " + id);
        ArticleVO articleVO = mvcArticleMapper.selectByArticleId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVO);
    }
}
